/*members addArtist, create, error, extractArtistDTO, 
    extractArtistInfoDTO, extractNode, firstChild, getElementsByTagName, 
    hasFirstChild, incrementSize, initialize, length, nodeValue, 
    parseArtistInfo, parseArtistSearch, prototype, setImage, setMbid, 
    setName, setSummary, setTotalResults, setUrl
 */
var XMLParser = Class.create();
XMLParser.prototype = {
		initialize : function() {

			this.hasFirstChild = function(node) {
				if (node !== null && node != UNDEFINED && node.firstChild !== null) {
					return true;
				}
				return false;
			};
			this.extractNode = function(parentNode, index, string) {
				var node;
				if (parentNode.length > 0) {
					node = parentNode[index].getElementsByTagName(string)[0];
				} else {
					node = parentNode.getElementsByTagName(string)[0];
				}
				return node;
			};
			this.extractArtistDTO = function(dto, artistsNode, index) {

				var name, url, image, mbid;

				name = this.extractNode(artistsNode, index, "name");
				if (this.hasFirstChild(name)) {
					dto.setName(name.firstChild.nodeValue);
				}
				url = this.extractNode(artistsNode, index, "url");
				if (this.hasFirstChild(url)) {
					dto.setUrl(url.firstChild.nodeValue);
				}
				mbid = this.extractNode(artistsNode, index, "mbid");
				if (this.hasFirstChild(mbid)) {
					dto.setMbid(mbid.firstChild.nodeValue);
				}
				image = this.extractNode(artistsNode, index, "image");
				if (this.hasFirstChild(image)) {
					dto.setImage(image.firstChild.nodeValue);
				}
			};

			this.extractArtistInfoDTO = function(dto, artistsNode, index) {

				var summary;
				this.extractArtistDTO(dto, artistsNode, index);

				summary = this.extractNode(artistsNode, 0, "summary");
				if (this.hasFirstChild(summary)) {
					dto.setSummary(summary.firstChild.nodeValue);
				}
			};

		},

		parseArtistSearch : function(xmlObject) {
			var artistList = new ArtistList();
			try {
				var totalResults = this.extractNode(xmlObject, 0, "opensearch:totalResults");
				if (totalResults!== null && this.hasFirstChild(totalResults)) {
					artistList.setTotalResults(totalResults.firstChild.nodeValue);
				}
				var artistsNode = xmlObject.getElementsByTagName('artist');
				for ( var index = 0; index < artistsNode.length; index++) {
					var artistDTO = new ArtistDTO();
					this.extractArtistDTO(artistDTO, artistsNode, index);
					artistList.addArtist(artistDTO);
					artistList.incrementSize();
				}
			} catch (e) {
				Logger.error("XMLParser parseArtistSearch " + e);
			}
			return artistList;
		},
		parseArtistInfo : function(xmlobject) {

			var artistInfoDTO = null;
			try {
				var artistNode = xmlobject.getElementsByTagName('artist')[0];
				artistInfoDTO = new ArtistInfoDTO();
				this.extractArtistInfoDTO(artistInfoDTO, artistNode);

			} catch (e) {
				Logger.error("XMLParser parseArtistInfo " + e);
			}
			return artistInfoDTO;
		}

};
